<template>
  <el-form label-width="100px">
    <el-form-item label="SPU名称">
      <el-input type="text" placeholder="SPU名称"></el-input>
    </el-form-item>
    <el-form-item label="品牌">
      <el-select placeholder="请选择" v-model="spuForm.tmId">
        <el-option
          v-for="trItem in trademarkList"
          :key="trItem.id"
          :value="trItem.id"
          :label="trItem.tmName"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="SPU描述">
      <el-input type="textarea" :rows="3" placeholder="SPU描述"></el-input>
    </el-form-item>
    <el-form-item label="SPU图片">
      <!-- v-model:file-list="fileList" -->
      <el-upload
        action="/app-dev/admin/product/upload"
        list-type="picture-card"
        :on-success="handlerSuccess"
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove"
      >
        <el-icon><Plus /></el-icon>
      </el-upload>

      <el-dialog v-model="dialogVisible">
        <img w-full :src="dialogImageUrl" alt="Preview Image" />
      </el-dialog>
    </el-form-item>
    <el-form-item label="销售属性">
      <div class="mb-10">
        <el-select placeholder="还有3个未选择" class="mr-10">
          <el-option
            v-for="bsa in baseSaleAttrList"
            :key="bsa.id"
            :value="bsa.id"
            :label="bsa.name"
          ></el-option>
          <el-option label="1" value="1"></el-option>
        </el-select>
        <el-button type="primary">添加销售属性</el-button>
      </div>
      <el-table :data="spuSaleAttrList" border>
        <el-table-column type="index" label="序号" align="center" width="80" />
        <el-table-column label="属性名" property="saleAttrName" width="200" />
        <el-table-column label="属性值名称列表"></el-table-column>
        <el-table-column label="操作" width="80">
          <template #default="{ row, $index }">
            <el-button type="danger" :icon="Delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary">保存</el-button>
      <el-button @click="cancelSave">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { Delete, Plus } from "@element-plus/icons-vue";
import { onMounted, ref } from "vue";
import type { UploadProps } from "element-plus";
import { ElMessage } from "element-plus";
import { SPUSTATUS } from "@/views/product/spu/index.vue";
import type {
  SpuModel,
  BaseSaleListModel,
  SpuImageListModel,
  SpuSaleAttrListModel,
} from "@/api/spu";
import spuApi from "@/api/spu";
import TrApi from "@/api/trademark";
import type { TrademarkListModel } from "@/api/trademark";
//接收父级v-model传过来的函数
const emits = defineEmits(["update:modelValue"]);
//获得父组件传过来的prop数据
const props = defineProps<{
  spuInfo: SpuModel;
}>();
const spuForm = props.spuInfo;
//点击取消回调
const cancelSave = () => {
  emits("update:modelValue", SPUSTATUS.SPULIST);
};

// 图片列表
const dialogVisible = ref(false); // 显示预览
const dialogImageUrl = ref(""); // 预览图片的url
// 上传成功的回调
const handlerSuccess: UploadProps["onSuccess"] = (response, uploadFile) => {
  console.log(response, uploadFile);
};
// 删除的回调
const handleRemove: UploadProps["onRemove"] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
};
// 预览的回调
const handlePictureCardPreview: UploadProps["onPreview"] = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url!;
  dialogVisible.value = true;
};

//点击编辑发送的请求
//1.品牌下拉的数据
const trademarkList = ref<TrademarkListModel>();
const getTrademarkList = async () => {
  try {
    const result = await TrApi.getList();
    trademarkList.value = result;
  } catch (e) {
    console.error(e);
    ElMessage.error("品牌下拉的数据");
    return Promise.reject(e);
  }
};
// 2.获取销售属性下拉
const baseSaleAttrList = ref<BaseSaleListModel>();
const getBaseSaleAttrList = async () => {
  try {
    const result = await spuApi.getSaleAttrList();
    baseSaleAttrList.value = result;
  } catch (e) {
    console.error(e);
    ElMessage.error("获取销售属性下拉失败");
    return Promise.reject(e);
  }
};
// 3.获取当前spu的图片列表
const spuImageList = ref<SpuImageListModel>();
const getSpuImageList = async () => {
  try {
    let result = await spuApi.getSpuImageList(spuForm.id as number);
    spuImageList.value = result;
    console.log("spuImageList", spuImageList.value);
  } catch (error) {
    console.error(error);
    ElMessage.error("获取SPU的图片列表失败");
  }
};
// 4.获取当前spu的销售属性列表
const spuSaleAttrList = ref<SpuSaleAttrListModel>();
const getSpuSaleAttrList = async () => {
  try {
    let result = await spuApi.getSpuSaleAttrList(spuForm.id as number);
    spuSaleAttrList.value = result;
    console.log("spuSaleAttrList", spuSaleAttrList.value);
  } catch (error) {
    console.error(error);
    ElMessage.error("获取SPU的销售属性列表失败");
  }
};
const initData = () => {
  if (spuForm.id) {
    getSpuImageList();
    getSpuSaleAttrList();
  }
  getTrademarkList();
  getBaseSaleAttrList();
};
onMounted(() => {
  initData();
});
</script>

<style scoped lang="css"></style>
